<extend name="public@base"/>
<block name="head-style">
    <style type="text/css">
        .pic-list li {
            margin-bottom: 5px;
        }
    </style>
</block>
<block name="head-script">
    <script type="text/html" id="photos-item-tpl">
        <li id="saved-image{id}">
            <input id="photo-{id}" type="hidden" name="photo_urls[]" value="{filepath}">
            <input class="form-control" id="photo-{id}-name" type="text" name="photo_names[]" value="{name}"
                   style="width: 200px;" title="图片名称">
            <img id="photo-{id}-preview" src="{url}" style="height:36px;width: 36px;"
                 onclick="imagePreviewDialog(this.src);">
            <a href="javascript:uploadOneImage('图片上传','#photo-{id}');">替换</a>
            <a href="javascript:(function(){$('#saved-image{id}').remove();})();">移除</a>
        </li>
    </script>
    <script type="text/html" id="files-item-tpl">
        <li id="saved-file{id}">
            <input id="file-{id}" type="hidden" name="file_urls[]" value="{filepath}">
            <input class="form-control" id="file-{id}-name" type="text" name="file_names[]" value="{name}"
                   style="width: 200px;" title="文件名称">
            <a id="file-{id}-preview" href="{preview_url}" target="_blank">下载</a>
            <a href="javascript:uploadOne('文件上传','#file-{id}','file');">替换</a>
            <a href="javascript:(function(){$('#saved-file{id}').remove();})();">移除</a>
        </li>
    </script>
</block>
<block name="main">
    <div class="wrap js-check-wrap">
        <ul class="nav nav-tabs">
            <li><a href="{:url('AdminArticle/index')}">文章管理</a></li>
            <li>
                <a href="{:url('AdminArticle/add')}">添加文章</a>
            </li>
            <li class="active"><a href="#">编辑文章</a></li>
        </ul>
        <form action="{:url('AdminArticle/editPost')}" method="post" class="form-horizontal js-ajax-form margin-top-20">
            <div class="row">
                <div class="col-md-9">
                    <table class="table table-bordered">
                        <tr>
                            <th width="100">分类<span class="form-required">*</span></th>
                            <td>
                                <input class="form-control" type="text" style="width:400px;" required
                                       value="{:implode(' ',$post_categories)}"
                                       placeholder="请选择分类" onclick="doSelectCategory();" id="js-categories-name-input"
                                       readonly/>
                                <input class="form-control" type="hidden" value="{$post_category_ids}"
                                       name="post[categories]"
                                       id="js-categories-id-input"/>
                            </td>
                        </tr>
                        <tr>
                            <th>标题<span class="form-required">*</span></th>
                            <td>
                                <input id="post-id" type="hidden" name="post[id]" value="{$post.id}">
                                <input class="form-control" type="text" name="post[post_title]"
                                       required value="{$post.post_title}" placeholder="请输入标题"/>
                            </td>
                        </tr>
                        <tr>
                            <th>关键词</th>
                            <td>
                                <input class="form-control" type="text" name="post[post_keywords]"
                                       value="{$post['post_keywords']}" placeholder="请输入关键字">
                                <p class="help-block">多关键词之间用英文逗号隔开</p>
                            </td>
                        </tr>
                        <tr>
                            <th>文章来源</th>
                            <td>
                                <input class="form-control" type="text" name="post[post_source]"
                                       value="{$post['post_source']}" placeholder="请输入文章来源">
                            </td>
                        </tr>
                        <tr>
                            <th>摘要</th>
                            <td>
                            <textarea class="form-control" name="post[post_excerpt]" style="height: 50px;"
                                      placeholder="请填写摘要">{$post.post_excerpt}</textarea>
                            </td>
                        </tr>
                        <tr>
                            <th>内容</th>
                            <td>
                                <script type="text/plain" id="content" name="post[post_content]">{$post.post_content}


                                </script>
                            </td>
                        </tr>
                        <tr>
                            <th>相册</th>
                            <td>
                                <ul id="photos" class="pic-list list-unstyled form-inline">
                                    <notempty name="post.more.photos">
                                        <foreach name="post.more.photos" item="vo">
                                            <php>$img_url=cmf_get_image_preview_url($vo['url']);</php>
                                            <li id="saved-image{$key}">
                                                <input id="photo-{$key}" type="hidden" name="photo_urls[]"
                                                       value="{$vo.url}">
                                                <input class="form-control" id="photo-{$key}-name" type="text"
                                                       name="photo_names[]"
                                                       value="{$vo.name|default=''}" style="width: 200px;" title="图片名称">
                                                <img id="photo-{$key}-preview"
                                                     src="{:cmf_get_image_preview_url($vo['url'])}"
                                                     style="height:36px;width: 36px;"
                                                     onclick="parent.imagePreviewDialog(this.src);">
                                                <a href="javascript:uploadOneImage('图片上传','#photo-{$key}');">替换</a>
                                                <a href="javascript:(function(){$('#saved-image{$key}').remove();})();">移除</a>
                                            </li>
                                        </foreach>
                                    </notempty>
                                </ul>
                                <a href="javascript:uploadMultiImage('图片上传','#photos','photos-item-tpl');"
                                   class="btn btn-sm btn-default">选择图片</a>
                            </td>
                        </tr>
                        <tr>
                            <th>附件</th>
                            <td>
                                <ul id="files" class="pic-list list-unstyled form-inline">
                                    <notempty name="post.more.files">
                                        <foreach name="post.more.files" item="vo">
                                            <php>$file_url=cmf_get_file_download_url($vo['url']);</php>
                                            <li id="saved-file{$key}">
                                                <input id="file-{$key}" type="hidden" name="file_urls[]"
                                                       value="{$vo.url}">
                                                <input class="form-control" id="file-{$key}-name" type="text"
                                                       name="file_names[]"
                                                       value="{$vo.name}" style="width: 200px;" title="图片名称">
                                                <a id="file-{$key}-preview" href="{$file_url}" target="_blank">下载</a>
                                                <a href="javascript:uploadOne('文件上传','#file-{$key}','file');">替换</a>
                                                <a href="javascript:(function(){$('#saved-file{$key}').remove();})();">移除</a>
                                            </li>
                                        </foreach>
                                    </notempty>
                                </ul>
                                <a href="javascript:uploadMultiFile('附件上传','#files','files-item-tpl','file');"
                                   class="btn btn-sm btn-default">选择文件</a>
                            </td>
                        </tr>
                        <tr>
                            <th>音频</th>
                            <td class="form-inline">
                                <input id="file-audio" class="form-control" type="text" name="post[more][audio]"
                                       value="{$post.more.audio|default=''}" placeholder="请上传音频文件"
                                       style="width: 200px;">
                                <notempty name="post.more.audio">
                                    <a id="file-audio-preview" href="{:cmf_get_file_download_url($post.more.audio)}"
                                       target="_blank">下载</a>
                                </notempty>

                                <a href="javascript:uploadOne('文件上传','#file-audio','audio');">上传</a>
                            </td>
                        </tr>
                        <tr>
                            <th>视频</th>
                            <td class="form-inline">
                                <input id="file-video" class="form-control" type="text" name="post[more][video]"
                                       value="{$post.more.video|default=''}" placeholder="请上传视频文件"
                                       style="width: 200px;">
                                <notempty name="post.more.video">
                                    <a id="file-video-preview" href="{:cmf_get_file_download_url($post.more.video)}"
                                       target="_blank">下载</a>
                                </notempty>
                                <a href="javascript:uploadOne('文件上传','#file-video','video');">上传</a>
                            </td>
                        </tr>
                    </table>

                    <hook name="portal_admin_article_edit_view_main"/>
                </div>
                <div class="col-md-3">
                    <table class="table table-bordered">
                        <tr>
                            <th>缩略图</th>
                        </tr>
                        <tr>
                            <td>
                                <div style="text-align: center;">
                                    <input type="hidden" name="post[more][thumbnail]" id="thumbnail"
                                           value="{$post.more.thumbnail|default=''}">
                                    <a href="javascript:uploadOneImage('图片上传','#thumbnail');">
                                        <if condition="empty($post.more.thumbnail)">
                                            <img src="__TMPL__/public/assets/images/default-thumbnail.png"
                                                 id="thumbnail-preview"
                                                 width="135" style="cursor: pointer"/>
                                            <else/>
                                            <img src="{:cmf_get_image_preview_url($post.more.thumbnail)}"
                                                 id="thumbnail-preview"
                                                 width="135" style="cursor: pointer"/>
                                        </if>
                                    </a>
                                    <input type="button" class="btn btn-sm btn-cancel-thumbnail" value="取消图片">
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <th>发布时间</th>
                        </tr>
                        <tr>
                            <td>
                                <input class="form-control js-bootstrap-datetime" type="text"
                                       name="post[published_time]"
                                       value="{:date('Y-m-d H:i',$post['published_time'])}">
                            </td>
                        </tr>
                        <!--
                        <tr>
                            <th>评论</th>
                        </tr>
                        <tr>
                            <td>
                                <label style="width: 88px"><a
                                        href="javascript:openIframeDialog('{:url('comment/commentadmin/index',array('post_id'=>$post['id']))}','评论列表')">查看评论</a></label>
                            </td>
                        </tr>
                        -->
                        <tr>
                            <th>状态</th>
                            <php>
                                $status_yes=$post['post_status']==1?"checked":"";
                                $is_top_yes=$post['is_top']==1?"checked":"";
                                $recommended_yes=$post['recommended']==1?"checked":"";
                            </php>
                        </tr>
                        <tr>
                            <td>
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" id="post-status-checkbox" name="post[post_status]"
                                               value="1"
                                               {$status_yes}>发布
                                        <span id="post-status-error" style="color: red;display: none"></span>
                                    </label>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <div class="checkbox">
                                    <label><input type="checkbox" id="is-top-checkbox" name="post[is_top]" value="1"
                                                  {$is_top_yes}>置顶</label>
                                    <span id="is-top-error" style="color: red;display: none"></span>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" id="recommended-checkbox" name="post[recommended]"
                                               value="1"
                                               {$recommended_yes}>推荐
                                        <span id="recommended-error" style="color: red;display: none"></span>
                                    </label>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <th>文章模板</th>
                        </tr>
                        <tr>
                            <td>
                                <select class="form-control" name="post[more][template]" id="more-template-select">
                                    <option value="">请选择模板</option>
                                    <foreach name="article_theme_files" item="vo">
                                        <php>$value=preg_replace('/^portal\//','',$vo['file']);</php>
                                        <option value="{$value}">{$vo.name} {$vo.file}.html</option>
                                    </foreach>
                                </select>
                            </td>
                        </tr>
                    </table>

                    <hook name="portal_admin_article_edit_view_right_sidebar"/>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="submit" class="btn btn-primary js-ajax-submit">{:lang('SAVE')}</button>
                    <a class="btn btn-default" href="javascript:history.back(-1);">{:lang('BACK')}</a>
                </div>
            </div>
        </form>
    </div>
</block>
<block name="bottom-script">
    <script type="text/javascript">
        //编辑器路径定义
        var editorURL = GV.WEB_ROOT;
    </script>
    <script type="text/javascript" src="__STATIC__/js/ueditor/ueditor.config.js"></script>
    <script type="text/javascript" src="__STATIC__/js/ueditor/ueditor.all.min.js"></script>
    <script type="text/javascript">
        $(function () {

            editorcontent = new baidu.editor.ui.Editor();
            editorcontent.render('content');
            try {
                editorcontent.sync();
            } catch (err) {
            }

            $('.btn-cancel-thumbnail').click(function () {
                $('#thumbnail-preview').attr('src', '__TMPL__/public/assets/images/default-thumbnail.png');
                $('#thumbnail').val('');
            });

            $('#more-template-select').val("{$post.more.template|default=''}");
        });

        function doSelectCategory() {
            var selectedCategoriesId = $('#js-categories-id-input').val();
            openIframeLayer("{:url('AdminCategory/select')}?ids=" + selectedCategoriesId, '请选择分类', {
                area: ['700px', '400px'],
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    //do something

                    var iframeWin = window[layero.find('iframe')[0]['name']];
                    var selectedCategories = iframeWin.confirm();
                    if (selectedCategories.selectedCategoriesId.length == 0) {
                        layer.msg('请选择分类');
                        return;
                    }
                    $('#js-categories-id-input').val(selectedCategories.selectedCategoriesId.join(','));
                    $('#js-categories-name-input').val(selectedCategories.selectedCategoriesName.join(' '));
                    //console.log(layer.getFrameIndex(index));
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                }
            });
        }
    </script>

    <script>

        var publishYesUrl = "{:url('AdminArticle/publish',array('yes'=>1))}";
        var publishNoUrl = "{:url('AdminArticle/publish',array('no'=>1))}";
        var topYesUrl = "{:url('AdminArticle/top',array('yes'=>1))}";
        var topNoUrl = "{:url('AdminArticle/top',array('no'=>1))}";
        var recommendYesUrl = "{:url('AdminArticle/recommend',array('yes'=>1))}";
        var recommendNoUrl = "{:url('AdminArticle/recommend',array('no'=>1))}";

        var postId = $('#post-id').val();

        //发布操作
        $("#post-status-checkbox").change(function () {
            if ($('#post-status-checkbox').is(':checked')) {
                //发布
                $.ajax({
                    url: publishYesUrl, type: "post", dataType: "json", data: {ids: postId}, success: function (data) {
                        if (data.code != 1) {
                            $('#post-status-checkbox').removeAttr("checked");
                            $('#post-status-error').html(data.msg).show();

                        } else {
                            $('#post-status-error').hide();
                        }
                    }
                });
            } else {
                //取消发布
                $.ajax({
                    url: publishNoUrl, type: "post", dataType: "json", data: {ids: postId}, success: function (data) {
                        if (data.code != 1) {
                            $('#post-status-checkbox').prop("checked", 'true');
                            $('#post-status-error').html(data.msg).show();
                        } else {
                            $('#post-status-error').hide();
                        }
                    }
                });
            }
        });

        //置顶操作
        $("#is-top-checkbox").change(function () {
            if ($('#is-top-checkbox').is(':checked')) {
                //置顶
                $.ajax({
                    url: topYesUrl, type: "post", dataType: "json", data: {ids: postId}, success: function (data) {
                        if (data.code != 1) {
                            $('#is-top-checkbox').removeAttr("checked");
                            $('#is-top-error').html(data.msg).show();

                        } else {
                            $('#is-top-error').hide();
                        }
                    }
                });
            } else {
                //取消置顶
                $.ajax({
                    url: topNoUrl, type: "post", dataType: "json", data: {ids: postId}, success: function (data) {
                        if (data.code != 1) {
                            $('#is-top-checkbox').prop("checked", 'true');
                            $('#is-top-error').html(data.msg).show();
                        } else {
                            $('#is-top-error').hide();
                        }
                    }
                });
            }
        });
        //推荐操作
        $("#recommended-checkbox").change(function () {
            if ($('#recommended-checkbox').is(':checked')) {
                //推荐
                $.ajax({
                    url: recommendYesUrl,
                    type: "post",
                    dataType: "json",
                    data: {ids: postId},
                    success: function (data) {
                        if (data.code != 1) {
                            $('#recommended-checkbox').removeAttr("checked");
                            $('#recommended-error').html(data.msg).show();

                        } else {
                            $('#recommended-error').hide();
                        }
                    }
                });
            } else {
                //取消推荐
                $.ajax({
                    url: recommendNoUrl, type: "post", dataType: "json", data: {ids: postId}, success: function (data) {
                        if (data.code != 1) {
                            $('#recommended-checkbox').prop("checked", 'true');
                            $('#recommended-error').html(data.msg).show();
                        } else {
                            $('#recommended-error').hide();
                        }
                    }
                });
            }
        });


    </script>

</block>

